package com.example.csupermaket.service.impl;


import com.example.csupermaket.exception.ServiceException;
import com.example.csupermaket.mapper.UserMapper;
import com.example.csupermaket.pojo.dto.UserAddNewDTO;
import com.example.csupermaket.pojo.dto.UserLoginDTO;
import com.example.csupermaket.pojo.dto.UserUpdateDTO;
import com.example.csupermaket.pojo.entity.User;
import com.example.csupermaket.pojo.vo.UserStandardVO;
import com.example.csupermaket.service.IUserService;
import com.example.csupermaket.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public void login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        String real = userMapper.getStandardByName(username).getPassword();
        if (!password.equals(real)){
            String message = "密码错误！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void addNew(UserAddNewDTO userAddNewDTO) {

        String username = userAddNewDTO.getUsername();
        int i = userMapper.countByName(username);
        if (i > 0) {
            String message = "添加失败，用户名称被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        User user = new User();

        BeanUtils.copyProperties(userAddNewDTO, user);

        userMapper.insert(user);
    }


    @Override
    public void updateInfoById(int uid, UserUpdateDTO userUpdateDTO) {

        UserStandardVO queryResult = userMapper.getStandardById(uid);

        if (queryResult == null) {
            // 是：数据不存在，抛出异常
            String message = "未找到对应用户，请检查输入的id是否正确！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String username = userUpdateDTO.getUsername();
        int count = userMapper.countByNameAndNotId(uid, username);
        if (count > 0) {
            String message = "修改失败，用户名称被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        user.setUid(uid);
        userMapper.updateInfoById(user);
    }

    @Override
    public void updatePasswordByUsername(String username, int phonenum) {
        UserStandardVO queryResult = getStandardByName(username);
        int realPhone = Integer.parseInt(queryResult.getPhonenum());
        if (phonenum != realPhone) {
            String message = "输入错误！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
    }

    @Override
    public UserStandardVO getStandardByName(String username) {
        UserStandardVO queryResult = userMapper.getStandardByName(username);
        if (queryResult == null) {
            String message = "未找到对应用户，请检查输入的用户名是否正确！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public UserStandardVO getStandardById(int uid) {
        UserStandardVO queryResult = userMapper.getStandardById(uid);
        if (queryResult == null) {
            String message = "未找到对应用户，请检查输入的id是否正确！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }


}
